package com.dlc.felear.xiaoensale.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.adapter.EmnuListtemAdapter1;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.SingleInputDialog;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.deviceUtil.TopbarHelper;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.helper.JsonHelper;
import com.itdlc.android.library.base.BaseActivity;
import com.itdlc.android.library.listener.TextWatcherAdapter;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;

/**
 * 配菜台 净重-运营商菜谱显示
 */
public class MenuList1Activity extends BaseActivity  implements SingleInputDialog.SingleDialogCallback
       {
    private static final String TAG="MenuList1Activity";
    @BindView(R.id.e_gv)
    GridView gv;
    @BindView(R.id.et_search)
    EditText etSeach;
    private CookingMenuDao cookingMenuDao;
    private MenuFoodMaterialDao foodMaterialDao;
    private EmnuListtemAdapter1 adapter;
    private CookingMenu cookingMenu;
    TopbarHelper topbarHelper;
    private SingleInputDialog singleInputDialog;
    private String carNum="";
    private String type="M400";


    @Override
    protected int getLayoutRes() {
        EventBus.getDefault().register(this);
        return R.layout.activity_menu_list_1;
    }

    @Override
    protected void initialView() {
        topbarHelper = new TopbarHelper(this);
        topbarHelper.setNavActivity(SuttleWeightActivity.class);
        topbarHelper.setcon(R.drawable.arrow_left);
        topbarHelper.setTitle("菜谱列表");
        adapter = new EmnuListtemAdapter1(this);
        singleInputDialog=new SingleInputDialog(this,this);
        singleInputDialog.setEtInputType(InputType.TYPE_CLASS_TEXT);
        gv.setAdapter(adapter);
        etSeach.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                super.beforeTextChanged(s, start, count, after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.setItems(cookingMenuDao.queryForLikeName(s.toString()),type);
            }

            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
            }
        });
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(TextUtils.isEmpty(carNum)){
                    cookingMenu = adapter.getData().get(position);
                    Log.e(TAG,"选择的菜谱:"+ JsonHelper.toJson(cookingMenu));
                    Intent intent = new Intent();
                    //把返回数据存入Intent
                    intent.putExtra("id", cookingMenu.getId());
                    //设置返回数据
                    setResult(1024, intent);
                    //关闭Activity
                    finish();
                }
            }
        });
        gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                cookingMenu = adapter.getData().get(position);
                if (cookingMenu != null) {
                    if(TextUtils.isEmpty(cookingMenu.getMacNo())){
                        singleInputDialog.setHintMsg("请输入绑定的磁卡编号");
                    }else {
                        singleInputDialog.setCarNo(cookingMenu.getMacNo());
                    }
                    singleInputDialog.show();
                }else {
                    showShortToast("数据异常");
                }
                return false;
            }
        });
    }


    @Override
    protected void initData() {
        cookingMenuDao = new CookingMenuDao(this);
        foodMaterialDao = new MenuFoodMaterialDao(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        type=getIntent().getStringExtra("type");
        initDaoData();
        Log.e(TAG,"type:"+type);
        carNum="";
    }

    private void initDaoData() {
        adapter.setItems(cookingMenuDao.queryForType(Constant.MENU_NORMAL),type);
    }


    @Override
    public void SingleDialogInputResult(String value, int id) {
        if(cookingMenu==null){
            return;
        }
        if(TextUtils.isEmpty(value)||TextUtils.equals(value,cookingMenu.getMacNo())){
            return;
        }
        if(TextUtils.equals(value,"1001")){
            deleteMenu(cookingMenu);
          return;
        }
        cookingMenu.setMacNo(value);
        showShortToast("设置成功");
    }

    private void deleteMenu(CookingMenu cookingMenu){
//        cookingMenuDao.deleteAll(cookingMenu);
        adapter.notifyDataSetChanged();
    }

    @Subscriber(tag = Constant.EVENTBUS_TAG_RFID_INFO)
    public void recieveRfid(String cardNum) {
        Log.e(TAG,"cardNum="+cardNum);
        if (!TextUtils.equals(cardNum, Constant.LEFT_CARD)) {
            this.carNum=cardNum;
            singleInputDialog.setCarNo(cardNum);
        }else {
            carNum="";
        }
    }

}